Building Connection Strings

Description

Load a default value for the connection string.

dim lv as P
dim mode as C
dim indx as N
dim con_name as C
dim con_string as C
dim current_con_name as C
dim current_con_string as C
dim flagEdit as L
dim con_filename as C
dim txt as C
dim a as P
lv = local_variables()
indx = 1
if (a5_is_path_valid(a5.Get_Exe_Path()+ chr(92) + "Learning Xdialog") = .f.) then
    dir_create_recurse(a5.Get_Exe_Path() + chr(92) + "Learning Xdialog")
end if
con_filename = a5.Get_Exe_Path()+ chr(92) + "Learning Xdialog\connections.txt"
if file.exists(con_filename) then
    txt = get_from_file(con_filename)
    property_from_string(a, txt)
else
    dim a.a_con[1] as P
end if

Display the Define Connections dialog box.

ui_dlg_box("Define Connections",<<%dlg%
{startup=init}
{font=tahoma,8}
{'%O={F=Tahoma,12,B}{C=Blue Gray}Connections:%};
{region0}
{region}
{lf};
[.40,15indx^#a.a_con[[\].description!indx_changed];
{endregion}|{sp}|
{frame=1,1:Connection String}
{region}
{text=50,15:current_con_string};
{endregion};
;
{endregion0};
<Close> <New> <Edit?flagEdit> <Delete?flagEdit>{sp=34}<Copy>;
%dlg%,<<%code%

Add the event handlers for the various buttons on the dialog box.

if a_dlg_button = "copy" then
    a_dlg_button = ""
    ClipBoard.Set_Data(current_con_string)
end if
if a_dlg_button = "delete" then
    a.a_con.delete(indx,1)
    a_dlg_button = ""
    if (indx > a.a_con.first_empty() - 1) then
        indx = indx - 1
    end if
    get_info(lv)
    save(lv)
end if
if a_dlg_button = "init" then
    a_dlg_button = ""
    get_info(lv)
end if
if a_dlg_button = "new" then
    a_dlg_button = ""
    mode = "New"
    new_connection(lv)
end if
if a_dlg_button = "edit" then
    a_dlg_button = ""
    mode = "Edit"
    new_connection(lv)
end if
if a_dlg_button = "indx_changed" then
    a_dlg_button = ""
    get_info(lv)
end if
%code%)

Add the function that retrieves the name and connection string for the selected profile.

function get_info as v (vars as P)
with vars
    if eval_valid("a.a_con[" + indx + "].name") = .f. then
        current_con_name = ""
        current_con_string = ""
        flagEdit = .f.
        exit function
    end if
    flagEdit = .t.
    current_con_name = a.a_con[indx].name
    current_con_string = a.a_con[indx].connection_string
end with
end function

Add the function that displays the New Connection dialog box.

function new_connection as v (vars as P)
dim flag_ok as L
dim dlg_title as C
with vars
flag_ok = .f.
if mode = "New" then
    dlg_title = "New Connection"
    conn_name = "Connection"+ a.a_con.first_empty()
    conn_string = ""
else if mode = "edit" then
    dlg_title = "Edit Connection"
    conn_name = current_con_name
    conn_string = current_con_string
end if
ui_dlg_box(dlg_title,<<%dlg%
{font=tahoma,8}
{on_key=enter}
Connection Name: ;
[.80conn_name];
{lf};
Connection String:;
[.80conn_string] <Build>;
{lf};
{line=1,0};
<10&OK!ok?.not.(conn_string="".or.conn_name="")> <10&Cancel>;
%dlg%,<<%code%

Add the event handlers for the buttons.

if (a_dlg_button = "build") then
    a_dlg_button = ""
    dim new_conn_string as C
    new_conn_string = a5_connectionString(conn_string)
    if new_conn_string <> "" then
        conn_string = new_conn_string
    end if
end if
if (a_dlg_button = "enter") then
    if (conn_string="".or.conn_name="") then
        ui_msg_box("Error","You have not defined the name, or the connection string.")
        a_dlg_button = ""
    else
        flag_ok = .t.
    end if
end if
if (a_dlg_button = "ok") then
    flag_ok = .t.
end if
%code%)
if (flag_ok = .t.) then
    dim next_slot as N
    if mode = "new" then
        next_slot = a.a_con.first_empty()
        if next_slot > a.a_con.size()then
            a.a_con.insert(next_slot,1)
        end if
    else
        next_slot = indx
    end if
    a.a_con[next_slot].name = conn_name
    a.a_con[next_slot].connection_string = conn_string
    a.a_con[next_slot].description = conn_name
    indx = next_slot
    save(lv)
end if
get_info(lv)
end with
end function

Add the function that saves a connection string.

function save as C (vars as P)
with vars
    dim txt as C
    txt = property_to_string(a)
    save_to_file(txt,con_filename)
end with
end function

Limitations

Desktop applications only.

See Also